package com.yxt.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.yxt.common.http.HttpUrlExtract;
import com.yxt.common.sys.CommConstant;
import com.yxt.common.sys.StringUtil;
import com.yxt.common.ui.ActivityStackControlUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;  
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

public class WaterQualityActivty extends Activity {
	private WebView webHtml;
	private TextView lblTitle;
	private Map<String,String> menuinfo = CommConstant.Main.FUNCTIONS.get(CommConstant.Main.CURRENT_TEMPLATE_CODE).get("SZREPORT");
	private Map<String,String> menuMap = new HashMap<String,String>();
	private String currentTemplate = CommConstant.RemoteFile.NOT_FOUND;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.water_quality);   //软件activity的布局	
		ActivityStackControlUtil.add(this);
		initComponent();
	}
	
	private void initComponent(){
		webHtml = (WebView)findViewById(R.id.webHtml);
		lblTitle = (TextView)findViewById(R.id.lblTitle);	
		Button btnBackHome = (Button)findViewById(R.id.btnBackHome);	
		btnBackHome.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MainActivity.class);
				startActivity(intent);
			}
		});

		Intent intent = getIntent();
		String itemId = intent.getStringExtra("itemId");
		Map<String, String> menu = CommConstant.Main.FUNCTIONS.get(CommConstant.Main.CURRENT_TEMPLATE_CODE).get(itemId);
		String server = menu.get("server");
		CommConstant.Sever.SZREPORT_SERVER = server;
		
		String params = menuinfo.get("parameters");
		if (StringUtil.isNotEmpty(params)) {
			params = params.substring(CommConstant.RemoteFile.DEFAULT_SUFFIX_LENGHT);
			String[] menus = params.split(CommConstant.RemoteFile.DEFAULT_MULTI_SPLITOR);
			for (int i = 0; i < menus.length; i++) {
				String[] array = menus[i].split(CommConstant.RemoteFile.DEFAULT_SINGLE_SPLITOR);
				menuMap.put(array[0], array[1]);
				if (i == 0) {
					currentTemplate = array[0];
				}
			}
			modifyTitle(currentTemplate);
		} else {
			lblTitle.setText(currentTemplate);
			webHtml.loadUrl(CommConstant.Config.ERROR_FILE_LOCATION); 		
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		Iterator<String> iterator = menuMap.keySet().iterator();
		int counts = 1;
		while (iterator.hasNext()) {
			menu.add(0, Menu.FIRST + counts, counts, iterator.next());
		}
		return super.onCreateOptionsMenu(menu);
	}
	
	/**
	 * 点击底部菜单项时的处理
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		modifyTitle(item.getTitle().toString());
		return super.onOptionsItemSelected(item);
	}

	private void modifyTitle(String title){
		lblTitle.setText(title);	
		String url = menuMap.get(title);
		String addr = CommConstant.Sever.SZREPORT_SERVER + url;
		if (HttpUrlExtract.checkUrlIsValidate(addr)) {		
			webHtml.loadUrl(addr); 
		} else {
			webHtml.loadUrl(CommConstant.Config.ERROR_FILE_LOCATION); 				
		}
	}

	protected void onDestroy() {
		super.onDestroy();
        ActivityStackControlUtil.remove(this);
	}
}